package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.CollectAdapter;
import flc.ast.bean.MyCollectBean;
import flc.ast.databinding.ActivityCollectBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

public class CollectActivity extends BaseAc<ActivityCollectBinding> {

    private CollectAdapter collectAdapter;
    private List<MyCollectBean> listShow=new ArrayList<>();
    private Dialog myClearDialog;


    @Override
    protected int onCreate() {
        return R.layout.activity_collect;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivCollectBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivCollectDelete.setOnClickListener(this);
        mDataBinding.rvCollectList.setLayoutManager(new GridLayoutManager(mContext,2));
        collectAdapter=new CollectAdapter();
        mDataBinding.rvCollectList.setAdapter(collectAdapter);
        collectAdapter.addChildClickViewIds(R.id.ivCollectItemCollect);
        collectAdapter.setOnItemChildClickListener(this);
        //清空弹框
        clearDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivCollectDelete:
                if (listShow.size()==0){
                    ToastUtils.showShort(getString(R.string.no_collect));
                    return;
                }
                myClearDialog.show();
                break;
            case R.id.ivDialogClearCancel:
                myClearDialog.dismiss();
                break;
            case R.id.ivDialogClearConfirm:
                listShow.clear();
                AppSpUtils.saveCollectList(listShow);
                getData();
                myClearDialog.dismiss();
                break;
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()){
            case R.id.ivCollectItemCollect:
                listShow.remove(collectAdapter.getItem(position));
                AppSpUtils.saveCollectList(listShow);
                getData();
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData(){
        listShow.clear();
        List<MyCollectBean> listSp = AppSpUtils.getCollectList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            collectAdapter.setList(listShow);
            mDataBinding.rvCollectList.setVisibility(View.VISIBLE);
            mDataBinding.llCollectNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvCollectList.setVisibility(View.GONE);
            mDataBinding.llCollectNoData.setVisibility(View.VISIBLE);
        }
    }

    //清空弹框
    private void clearDialog() {
        myClearDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_clear, null);
        myClearDialog.setContentView(inflate);
        myClearDialog.setCancelable(true);
        //获取当前Activity所在的窗体
        Window dialogWindow = myClearDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogConfirm = inflate.findViewById(R.id.ivDialogClearConfirm);
        ImageView dialogCancel = inflate.findViewById(R.id.ivDialogClearCancel);
        dialogConfirm.setOnClickListener(this);
        dialogCancel.setOnClickListener(this);
    }

}